/*
 *  @ProductName@
 *  Copyright (C) @CopyrightYear@
 *    National Institute of Advanced Industrial Science and Technology (AIST)
 *    Registration Number: @AISTRegistrationNumber@
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jp.go.aist.six.test.util.castor.persist;

import jp.go.aist.six.util.castor.CastorDao;
import jp.go.aist.six.util.persist.PersistenceException;
import java.util.Vector;



/**
 * @author  Akihito Nakamura, AIST
 * @version $Id: StateDao.java 161 2010-06-28 14:24:23Z nakamura5akihito $
 */
public class ProductDao
    extends CastorDao<Integer, Product>
{

    public ProductDao()
    {
        super( Product.class );
    }



//    //**************************************************************
//    //  Dao, CastorDao
//    //**************************************************************


    @Override
    protected void _daoBeforeCreate(
                    final Product object
                    )
    throws PersistenceException
    {
        ProductGroup  group = object.getGroup();
        if (group != null) {
            ProductGroup  p_group = _daoLoadOrCreate( ProductGroup.class, group );
            if (p_group != null  &&  p_group != group) {
                object.setGroup( p_group );
            }
        }

        Vector<Category>  categories = object.getCategories();
        if (categories != null  &&  categories.size() > 0) {
            for (Category  c : categories) {
                Category  p_c = _daoLoadOrCreate( Category.class, c );
                if (p_c != null  &&  p_c != c) {
                    p_c.addProduct( object );
                }
            }
        }
    }



    @Override
    protected void _daoBeforeUpdate(
                    final Product object
                    )
    throws PersistenceException
    {
        ProductGroup    group = object.getGroup();
        if (group != null) {
            _update( ProductGroup.class, group );
        }

        Vector<Category>  categories = object.getCategories();
        for (Category  category : categories) {
            _update( Category.class, category );
        }
    }



    @Override
    protected void _syncProperties(
                    final Product object,
                    final Product p_object
                    )
    {
        p_object.setName( object.getName() );
        p_object.setPrice( object.getPrice() );

        p_object.getDetails().clear();
        for (ProductDetail  detail : object.getDetails()) {
            p_object.addDetail( detail );
        }
    }



    @Override
    protected void _daoBeforeSync(
                    final Product   object,
                    final Product p_object
                    )
    throws PersistenceException
    {
        super._daoBeforeSync( object, p_object );

        ProductGroup    group = object.getGroup();
        ProductGroup  p_group = null;
        if (group != null) {
            p_group = _sync( ProductGroup.class, group );
//            p_group = getForwardingDao( ProductGroup.class ).sync( group );
        }

        if (p_object == null) {
            if (p_group == null) {
            } else {
                object.setGroup( p_group );
            }
        } else {
            if (p_group == null) {
                p_object.setGroup( group );
            } else {
                p_object.setGroup( p_group );
            }
        }

        Vector<Category>  categories = new Vector<Category>( object.getCategories() );
        if (p_object == null) {
            object.getCategories().clear();
        } else {
            p_object.getCategories().clear();
        }
        for (Category  category : categories) {
            Category  p_category = _sync( Category.class, category );
            if (p_object == null) {
                if (p_category == null) {
                    object.addCategories( category );
                } else {
                    object.addCategories( p_category );
                }
            } else {
                if (p_category == null) {
                    p_object.addCategories( category );
                } else {
                    p_object.addCategories( p_category );
                }
            }
        }
    }

}
// ProductDao
